package org.compiere.model;

import java.io.File;
import java.math.BigDecimal;
import java.util.Properties;

import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine;
import org.compiere.process.ProcessInfo;
import org.compiere.util.ASyncProcess;

public class MCMSContract extends X_CMS_Contract implements DocAction, ASyncProcess 
{
	private static final long serialVersionUID = -1716189430400550431L;
	/**	Process Message 			*/
	private String		m_processMsg = null;
	
	public MCMSContract(Properties ctx) 
	{
		super(ctx);
	}

	public MCMSContract(Properties ctx, int CMS_Contract_ID, String trxName)
	{
		super(ctx, CMS_Contract_ID, trxName);
	}
	
	@Override
	public boolean processIt(String processAction) throws Exception {
		m_processMsg = null;
		
		String docAction = getDocAction();
		String docStatus = getDocStatus();
		
		if(docStatus.equals(MCMSContract.ACTION_WaitComplete) && !docAction.equals(MCMSContract.ACTION_ReActivate) && !docAction.equals(MCMSContract.ACTION_Reverse_Accrual))
		{
			CreateDeposit();
		}
		
		DocumentEngine engine = new DocumentEngine (this, getDocStatus());
		return engine.processIt (processAction, getDocAction());
	}

	@Override
	public boolean unlockIt() {
		log.info("unlockIt - " + toString());
		return true;
	}

	@Override
	public boolean invalidateIt() {
		log.info("invalidateIt - " + toString());
		setDocAction(DOCACTION_Prepare);
		return true;
	}

	@Override
	public String prepareIt() {
		log.info("CMS Contract Bussines Process. prepareIt()" + toString());
		return DocAction.STATUS_InProgress;
	}

	@Override
	public boolean approveIt() {
		log.info(toString());
		//setIsApproved(true);
		return true;
	}

	@Override
	public boolean rejectIt() {
		log.info(toString());
		//setIsApproved(false);
		return true;
	}

	@Override
	public String completeIt() {
		log.info("CMS Contract Bussines Process. completeIt()" + toString());
		return DocAction.STATUS_Completed;
	}

	@Override
	public boolean voidIt() {
		log.info("CMS Contract Bussines Process. voidIt()" + toString());
		return true;
	}

	@Override
	public boolean closeIt() {
		log.info("CMS Contract Bussines Process. closeIt()" + toString());
		return true;
	}

	@Override
	public boolean reverseCorrectIt() {
		log.info("CMS Contract Bussines Process. reverseCorrectIt()" + toString());
		return true;
	}

	@Override
	public boolean reverseAccrualIt() {
		log.info("CMS Contract Bussines Process. reverseAccrualIt()" + toString());
		return true;
	}

	@Override
	public boolean reActivateIt() {
		log.info("CMS Contract Bussines Process. reActivateIt()" + toString());
		log.info(toString());
		// Before reActivate
		m_processMsg = ModelValidationEngine.get().fireDocValidate(this,ModelValidator.TIMING_BEFORE_REACTIVATE);
		if (m_processMsg != null)
			return false;	
		
		if (! reverseCorrectIt())
			return false;

		// After reActivate
		m_processMsg = ModelValidationEngine.get().fireDocValidate(this,ModelValidator.TIMING_AFTER_REACTIVATE);
		if (m_processMsg != null)
			return false;				
		
		return true;
	}

	private void CreateDeposit()
	{
		MTRMDeposit[] deposits = MTRMDeposit.getOfCMS_Contract(getCtx(), this.get_ID(), get_TrxName());
		if(deposits.length > 0 || !this.isDeposits()) return;
		
		MTRMDeposit deposit = new MTRMDeposit(getCtx(), null, get_TrxName());
		
		deposit.setCMS_Contract_ID(this.get_ID());
		deposit.setDateSigning(this.getCMS_Contract_CreatDate());
		deposit.setDescription(this.getDescription());
		deposit.setSum(this.getContractSummary());
		deposit.setInterestRate(this.getrateremun());
		deposit.setEstablishSubLimit(this.getEstablishSubLimit());
		deposit.setMinBalance(this.getMinBalance());
		deposit.setisPartialWithdrawal(this.isPartialWithdrawal());
		deposit.setBeginningDate(this.getBeginningDateExecution());
		deposit.setEndDate(this.getEndDateExecution());
		
		deposit.saveEx();		
	}
	
	protected boolean afterSave (boolean newRecord, boolean success)
	{
		CreateDeposit();		
		return true;
	}	//	afterSave

	@Override
	public String getDocumentInfo() {
		return null;
	}

	@Override
	public File createPDF() {
		return null;
	}

	@Override
	public String getProcessMsg() {
		return "";
	}

	@Override
	public int getDoc_User_ID() {
		return 0;
	}

	@Override
	public BigDecimal getApprovalAmt() {
		return null;
	}

	@Override
	public void lockUI(ProcessInfo pi) {
		
	}

	@Override
	public void unlockUI(ProcessInfo pi) {
		
	}

	@Override
	public boolean isUILocked() {
		return false;
	}

	@Override
	public void executeASync(ProcessInfo pi) {
		
	}

	@Override
	public String getSummary() {
		// TODO Auto-generated method stub
		return null;
	}

}
